package com.woyou.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.Detail;
import com.woyou.bean.Goods;
import com.woyou.bean.Option;
import com.woyou.bean.SubGoods;
import com.woyou.bean.Title;
import com.woyou.model.PropPanelModel;
import com.woyou.model.PropPanelModel_;
import com.woyou.ui.component.fastscroll.IViewHolder;
import com.woyou.utils.ParseUtils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PropertyAdapter extends BaseAdapter implements OnClickListener {
	private static final String TAG = "PropertyAdapter";
	public Context context;
	public List<Object> objList = new ArrayList<Object>();
	private TextView price;
	private Goods goods;
	private PropPanelModel mPropPanelController;
	private Drawable rediobox1;
	private Drawable rediobox2;
	private Drawable checkbox1;
	private Drawable checkbox2;
	
	private static final int TYPE_MAX_COUNT = 4;
	private static final int TYPE_TITLE = 0;
	private static final int TYPE_DETAIL = 1;
	private static final int TYPE_OPTION = 2;
	private static final int TYPE_SUBGOODS = 3;

	public PropertyAdapter(Context context, List<Object> objList, TextView price) {
		this.context = context;
		this.objList = objList;
		this.price = price;
		mPropPanelController = PropPanelModel_.getInstance_(context);
		Resources res = context.getResources();
		Bitmap temp1 = BitmapFactory.decodeResource(res, R.raw.rediobox1);
		rediobox1 = new BitmapDrawable(res, temp1);
		Bitmap temp2 = BitmapFactory.decodeResource(res, R.raw.rediobox2);
		rediobox2 = new BitmapDrawable(res, temp2);
		Bitmap temp3 = BitmapFactory.decodeResource(res, R.raw.checkbox1);
		checkbox1 = new BitmapDrawable(res, temp3);
		Bitmap temp4 = BitmapFactory.decodeResource(res, R.raw.checkbox2);
		checkbox2 = new BitmapDrawable(res, temp4);
	}

	/**
	 * 设置Goods
	 */
	public void setGoods(Goods goods){
		this.goods=goods;
	}

	/**
	 * 属性的ViewHolder
	 * 
	 * @author 荣
	 * 
	 */
	class ChildViewHolder implements IViewHolder {
		// title
		RelativeLayout property_view;
		TextView property_name;
		// 明细
		RelativeLayout details_view;
		TextView details_name;
		// 选项
		RelativeLayout option_view;
		TextView option_name;
		TextView option_price;
		CheckBox option_cb;
		CheckBox option_wy;
		// 子商品
		RelativeLayout subgoods_view;
		TextView subgoods_name;
		TextView subgoods_price;
		ImageView subgoods_add;
		TextView subgoods_num;
		ImageView subgoods_minus;

		SubGoods subGoods;
		Option opt;
		@Override
		public void load() {
			// TODO Auto-generated method stub
			
		}
	}

	@Override
	public int getCount() {
		return objList.size();
	}

	@Override
	public Object getItem(int position) {
		Log.i(TAG, "getItem() position=" + position);
		return objList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// Log.i(TAG, "position="+position);
		return position;
	}
	
	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}
	
	@Override
	public int getItemViewType(int position) {
		Object obj = objList.get(position);
		int type = -1;
		if( obj instanceof Title ){
			type = TYPE_TITLE;
		} else if (obj instanceof Detail){
			type = TYPE_DETAIL;
		} else if (obj instanceof Option){
			type = TYPE_OPTION;
		}else if (obj instanceof SubGoods){
			type = TYPE_SUBGOODS;
		}
		return type;
	}

	int count = 0;
	ChildViewHolder viewHolder;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i(TAG, "position=" + position + "  count=" + count);
		count++;
		 int type = getItemViewType(position);
		if (convertView == null) {
			Log.i("convertView", "convertView == null");
			viewHolder = new ChildViewHolder();
			switch (type) {
			case TYPE_TITLE:
				convertView = View.inflate(context,R.layout.item_property_group_title, null);
				// title
				viewHolder.property_view = (RelativeLayout) convertView
						.findViewById(R.id.item_property_view);
				viewHolder.property_name = (TextView) convertView
						.findViewById(R.id.item_property_name);
				break;
			case TYPE_DETAIL:
				convertView = View.inflate(context,R.layout.item_property_details, null);
				// 明细
				viewHolder.details_view = (RelativeLayout) convertView
						.findViewById(R.id.item_details_view);
				viewHolder.details_name = (TextView) convertView
						.findViewById(R.id.item_details_name);
				break;
			case TYPE_OPTION:
				convertView = View.inflate(context,R.layout.item_property_option, null);
				// 选项
				viewHolder.option_view = (RelativeLayout) convertView
						.findViewById(R.id.item_option_view);
				viewHolder.option_name = (TextView) convertView
						.findViewById(R.id.item_option_name);
				viewHolder.option_price = (TextView) convertView
						.findViewById(R.id.item_option_price);
				viewHolder.option_cb = (CheckBox) convertView
						.findViewById(R.id.item_option_cb);
				viewHolder.option_wy = (CheckBox) convertView
						.findViewById(R.id.item_option_wy);
				break;
			case TYPE_SUBGOODS:
				convertView = View.inflate(context,R.layout.item_property_subgoods, null);
				// 子商品
				viewHolder.subgoods_view = (RelativeLayout) convertView
						.findViewById(R.id.item_subgoods_view);
				viewHolder.subgoods_name = (TextView) convertView
						.findViewById(R.id.item_subgoods_name);
				viewHolder.subgoods_price = (TextView) convertView
						.findViewById(R.id.item_subgoods_price);
				viewHolder.subgoods_add = (ImageView) convertView
						.findViewById(R.id.item_subgoods_add);
				viewHolder.subgoods_num = (TextView) convertView
						.findViewById(R.id.item_subgoods_num);
				viewHolder.subgoods_minus = (ImageView) convertView
						.findViewById(R.id.item_subgoods_minus);
				break;

			default:
				break;
			}
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ChildViewHolder) convertView.getTag();
		}

		Object obj = objList.get(position);
		//

		if (obj instanceof Title) {
			// Title
			viewHolder.property_view.setVisibility(View.VISIBLE);
			Title title = (Title) obj;
			viewHolder.property_name.setText("" + title.getTitle());
		} else if (obj instanceof Detail) {
			// 明细
			viewHolder.details_view.setVisibility(View.VISIBLE);
			Detail detail = (Detail) obj;
			viewHolder.details_name.setText("" + detail.getDetail());
		} else if (obj instanceof Option) {
			// 选项
			viewHolder.option_view.setVisibility(View.VISIBLE);
			Option option = (Option) obj;
			viewHolder.opt = option;
			viewHolder.option_view.setTag(viewHolder);
			viewHolder.option_wy.setTag(viewHolder);
			viewHolder.option_cb.setTag(viewHolder);
			viewHolder.option_view.setOnClickListener(this);
			viewHolder.option_cb.setOnClickListener(this);
			viewHolder.option_wy.setOnClickListener(this);
			viewHolder.option_name.setText("" + option.getOptName());

			// 如果是规格组或者属性组的价格不为0则显示价格
			if (option.isSpec()) {
				viewHolder.option_price.setText(Html.fromHtml("<small>￥</small>"+ ParseUtils.String2Money(option.getSpecPrice())));
			} else if (option.getPrice() > 0) {
				viewHolder.option_price.setText(Html.fromHtml("+<small>￥</small>"+ ParseUtils.String2Money(option.getPrice())));
			} else {
				viewHolder.option_price.setText("");
			}
			// 是否判断是否显示单选按钮或者多选按钮
			if (option.isSpec() || (option.getParent().getSelectNum() == 1)) {
				viewHolder.option_cb.setVisibility(View.VISIBLE);
				viewHolder.option_wy.setVisibility(View.GONE);
			} else {
				viewHolder.option_cb.setVisibility(View.GONE);
				viewHolder.option_wy.setVisibility(View.VISIBLE);
			}
			//M选N并且M=1
			if(option.isSpec()&&option.getParent().getOptList().size()==1){
				option.setChecked(true);
				viewHolder.option_cb.setVisibility(View.VISIBLE);
				viewHolder.option_wy.setVisibility(View.GONE);
			}
			
			if (option.isChecked()) {
				viewHolder.option_wy.setBackgroundDrawable(checkbox2);
				viewHolder.option_cb.setBackgroundDrawable(rediobox1);
			} else {
				viewHolder.option_wy.setBackgroundDrawable(checkbox1);
				viewHolder.option_cb.setBackgroundDrawable(rediobox2);
			}

		} else if (obj instanceof SubGoods) {
			// 子商品
			viewHolder.subgoods_view.setVisibility(View.VISIBLE);
			SubGoods subGoods = (SubGoods) obj;

			viewHolder.subGoods = subGoods;
			viewHolder.subgoods_add.setTag(viewHolder);
			viewHolder.subgoods_minus.setTag(viewHolder);
			viewHolder.subgoods_name.setText("" + subGoods.getgName());
			viewHolder.subgoods_price.setText(Html.fromHtml("<small>￥</small>"+ ParseUtils.String2Money(subGoods.getPrice())));
			refSubGoodsNum(viewHolder);
			viewHolder.subgoods_add.setOnClickListener(this);
			viewHolder.subgoods_minus.setOnClickListener(this);
		}
		return convertView;
	}

	private void refSubGoodsNum(ChildViewHolder viewHolder) {
		if (viewHolder.subGoods.getChosenNum() <= 0) {
			viewHolder.subgoods_num.setVisibility(View.GONE);
			viewHolder.subgoods_minus.setVisibility(View.GONE);
		} else {
			viewHolder.subgoods_num.setVisibility(View.VISIBLE);
			viewHolder.subgoods_minus.setVisibility(View.VISIBLE);
		}
		viewHolder.subgoods_num.setText("" + viewHolder.subGoods.getChosenNum());
	}

	@Override
	public void onClick(View v) {
		ChildViewHolder viewHolder = (ChildViewHolder) v.getTag();
		switch (v.getId()) {
		case R.id.item_option_wy:
		case R.id.item_option_cb:
		case R.id.item_option_view:
			Option opt = viewHolder.opt;
			// OptionGroup parent = opt.getParent();

			if (opt.isSpec()||(opt.getParent().isRequired())) {
				// 用户点击规格组已被选中的属性 不做任何操作
				if (opt.isChecked()) {
//					notifyDataSetChanged();// TODO 需要优化
					return;
				}
			}
			mPropPanelController.changeOpt(opt);
			// Option remove = parent.changeOpt(opt);
			// if( remove!=null && !opt.equals(remove) ){
			// chosenGoods.changeOpt(remove);
			// }
			// chosenGoods.changeOpt(opt);
			notifyDataSetChanged();
			price.setText(Html.fromHtml("<small>￥</small>"+ ParseUtils.formatPrice(mPropPanelController.getPrice())+"<small><font color='#5b5b5b'>/"+goods.getUnit()+"</font></small>"));
			break;
		// 子商品
		case R.id.item_subgoods_add:
			mPropPanelController.addSubGoods(viewHolder.subGoods);
			// chosenGoods.addSubGoods(viewHolder.subGoods);
			price.setText(Html.fromHtml("<small>￥</small>"+ ParseUtils.formatPrice(mPropPanelController.getPrice())+"<small><font color='#5b5b5b'>/"+goods.getUnit()+"</font></small>"));
			refSubGoodsNum(viewHolder);
			// notifyDataSetChanged();
			break;
		case R.id.item_subgoods_minus:
			mPropPanelController.minusSubGoods(viewHolder.subGoods);
			// chosenGoods.minusSubGoods(viewHolder.subGoods);
			price.setText(Html.fromHtml("<small>￥</small>"+ ParseUtils.formatPrice(mPropPanelController.getPrice())+"<small><font color='#5b5b5b'>/"+goods.getUnit()+"</font></small>"));
			refSubGoodsNum(viewHolder);
//			notifyDataSetChanged();
			break;
		}

	}

	@Override
	public void notifyDataSetChanged() {
		count = 0;
		Log.i("notifyDataSetChanged", "notifyDataSetChanged");
		super.notifyDataSetChanged();
	}

}
